package com.inossem.warehousestorage_inossem.activity.change_storage.chuangyou;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.LoginActivity;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.ChangeStorageMaterielDetailActivity;
import com.inossem.warehousestorage_inossem.activity.change_storage.extra.ChangeStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.changeStorage.chuangyou.CYChangeStorageListAdapter;
import com.inossem.warehousestorage_inossem.adapter.changeStorage.chuangyou.CYChangeStorageMaterialDetailtAdapter;
import com.inossem.warehousestorage_inossem.adapter.changeStorage.chuangyou.CYChangeStorageSelectReceiveMaterialAdapter;
import com.inossem.warehousestorage_inossem.application.WarehouseStorageApplication;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageGetBatchRequestBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageMaterialBatchBean;
import com.inossem.warehousestorage_inossem.bean.changeStorage.chuangyou.CYChangeStorageMaterialListBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.BatchBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.changeStorage.chuangyou.CYChangeStorageMaterialBatchRequest;
import com.inossem.warehousestorage_inossem.request.changeStorage.chuangyou.CYChangeStorageMaterialListRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.CYChangeStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.List;

/**
 * TODO
 *
 * @author 王斯宇
 * @time on 2018/3/22 11:53
 * @email siyu.wang@inossem.com
 */
public class CYChangeStorageMaterielBatchDetailActivity extends BaseActivity {


    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView materielList;

    private CYChangeStorageMaterialDetailtAdapter adapter;

    private CYChangeStorageBean changeStorageBean;

    private CYChangeStorageGetBatchRequestBean batchRequestBean;

    private String matId , supplierCode , mCondition , location , locationId;
    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.cy_activity_change_storage_materiel_batch_detail);
        initView(CYChangeStorageMaterielBatchDetailActivity.this);
        initData(CYChangeStorageMaterielBatchDetailActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        materielList = (PullableListView) activity.findViewById(R.id.materielList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.materielListRefresh);

    }
    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.activity_receive_materiel_detail_title));
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_receive_result_back), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
//                backAndUpdate();
                finish();
            }
        });
        //初始化右上角全部批次按钮
        ActivityUtils.initRightFunction(activity, getRightFunction(activity), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                getData(CYChangeStorageMaterielBatchDetailActivity.this);
            }
        });
        getExtras();
        materielList.setPullUpFlag(false);
        materielList.setPullDownFlag(false);
        changeStorageBean = CYChangeStorageUtils.getChangeStorageBean(activity);
        location = changeStorageBean.getLocationCode() + "-" +changeStorageBean.getLocationName();
        locationId = changeStorageBean.getLocationId();
        mCondition  = "";
        batchRequestBean = CYChangeStorageUtils.getBatchRequestBean(activity , matId , mCondition);
        setmScanListener((String scanResult, int length) -> {
            mCondition = QRCodeUtils.DecryptQRCode(scanResult);
//            getData(activity, mCondition, mFactoryCode, mInventoryAddressCode, mBusinessType, mSpecialInventory);
        }, QRCodeConstant.TYPE_MATERIEL_CODE);
        addChangeStorageMaterielListData(activity , CYChangeStorageUtils.getMaterialDetailById(activity ,matId , supplierCode , false) , true);
    }

    private void getExtras () {
        matId = getIntent().getStringExtra(ChangeStorageExtra.MAT_ID);
        supplierCode = getIntent().getStringExtra(ChangeStorageExtra.SUPPLIER_CODE);
    }

    private View getRightFunction(Context context) {
        View rightFunctionView = LayoutInflater.from(context).inflate(R.layout.cy_view_material_detail_right_text, null);
        return rightFunctionView;
    }
    private void addChangeStorageMaterielListData(Context context, ItemListBean materialBeanList , boolean flag) {
        if (flag) {
            adapter = new CYChangeStorageMaterialDetailtAdapter(context, mCondition, location , materialBeanList  , new ChangeStorageMaterielListener() {
                @Override
                public void onNoDoubleClickListener(String condition ) {
                    mCondition = condition;
                    getData(context);
                }

                @Override
                public void onLongClickListener(String batch) {

                }
            });
            materielList.setAdapter(adapter);
        }
    }
    private void getData (Context context ) {
        String materialList = SPUtils.getChangeStorageMaterielList(context);


        CYChangeStorageMaterialBatchRequest.request(context,batchRequestBean, new GsonBeanListener<BatchBean>() {
                @Override
                public void onSuccess(BatchBean bean) {
                    addChangeStorageMaterielListData(context , CYChangeStorageUtils.getMaterialDetailById(context ,matId , supplierCode , bean.getBody() , false) , true);
                }
            });
//            Gson gson = new Gson();
//            CYChangeStorageMaterialListBean bean = gson.fromJson(materialList, CYChangeStorageMaterialListBean.class);
//            if (TextUtils.isEmpty(mCondition)) {
//                addChangeStorageMaterielListData(context , bean.getBody() , true);
//            }else {
//                addChangeStorageMaterielListData(context , CYChangeStorageUtils.getMaterialItem(context , mCondition) , true);
//            }

    }
    private void backAndUpdate() {
//        String checkResult = check(CYChangeStorageMaterielBatchDetailActivity.this);
//        if (checkResult == null) {
//            updateInfo(CYChangeStorageMaterielBatchDetailActivity.this);
//            super.onBackPressed();
//        } else {
//            DialogUtils.getConfirmDialog(CYChangeStorageMaterielBatchDetailActivity.this, checkResult + "\n" + getResources().getString(R.string.activity_change_storage_materiel_detail_whether_to_force_out), new DialogUtils.ConfirmDialogListener() {
//                @Override
//                public void onConfirm(Dialog dialog) {
//                    CYChangeStorageMaterielBatchDetailActivity.super.onBackPressed();
//                }
//            }).show();
//        }
    }

    @Override
    public void onBackPressed() {
        backAndUpdate();
    }

    public interface ChangeStorageMaterielListener{
        void onNoDoubleClickListener(String condition);
        void onLongClickListener(String batch);
    }
}
